package weather.app.pack;

import java.util.ArrayList;
import java.util.List;

import java.util.Collections;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

class CityAdapter extends BaseAdapter
{
	private Activity context;
	List<String> cities;
 
	/**
	 * 
	 * @param _context
	 */
	public CityAdapter (Activity _context)
	{
		this.context = _context;
		cities = new ArrayList<String>();
		
	}
	  
	@Override
	public View getView (int position, View convertView, ViewGroup list) 
	{
		// functia trebuie sa intoarca view-ul de pe pozitia position din lista
		// convertView este un element din lista ce nu mai este vizibil si poate fi convertit
		View element;
		if (convertView == null)
		{
			LayoutInflater inflater = context.getLayoutInflater();
			element = inflater.inflate(R.layout.city, null);
		}
		else 
		{
			element = convertView;
		}
	 
		TextView name = (TextView)element.findViewById(R.id.city_name);

		name.setText(cities.get(position));

		return element;
	}// end getView
 
	/**
	 * returns number of cities
	 */
	@Override
	public int getCount ()
    {
		// intaorce nr de elemente din lista
		return cities.size ();
	}
 
	/**
	 * returns city from position
	 */
	@Override
	public Object getItem(int position) 
	{
		// intoarce elementul de pe pozitia position din model
		return cities.get(position);
	}
	
	/**
	 * fiecare element din lista poate avea un id, nu este insa obligatoriu
	 */
	@Override
	public long getItemId(int position) 
	{
		return 0;
	}
 
	/**
	 * Add New City
	 * @param name
	 */
    public void addCity (String name)
    {
    	cities.add(name);
    	
    	//sort
    	Collections.sort(cities);
        
    	//refresh content
    	this.notifyDataSetChanged();
    }
        
       /**
        * Remove city
        * @param position
        */
    public void removeCity(int position)
    {
    	cities.remove(position);
        		
    	this.notifyDataSetChanged();
        	
        
    }
		
}